package com.xinsoft.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.QuaCheckPlanParam;
import com.xinsoft.entity.po.QuaCheckPlan;
import com.xinsoft.mapper.QuaCheckPlanMapper;
import com.xinsoft.service.QuaCheckPlanService;
import com.xinsoft.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author lijie
* @description 针对表【qua_check_plan(检验方案)】的数据库操作Service实现
* @createDate 2023-07-28 15:58:56
*/
@Service
public class QuaCheckPlanServiceImpl extends ServiceImpl<QuaCheckPlanMapper, QuaCheckPlan>
    implements QuaCheckPlanService{

    @Resource
    private SysUserService userService;

    @Override
    public Object findPageListByParam(QuaCheckPlanParam param) {
        //分页
        Page<QuaCheckPlan> page = new Page<QuaCheckPlan>(param.getCurrentPage(), param.getPageSize());
        QueryWrapper<QuaCheckPlan> queryWrapper = new QueryWrapper<>();
        //查询条件
        if(StrUtil.isNotBlank(param.getCheckPlanName())){
            queryWrapper.like("check_plan_name", param.getCheckPlanName());
        }
        //查询条件-是否启用
        if(param.getAuditStatus() != null){
            queryWrapper.eq("audit_status", param.getAuditStatus());
        }
        queryWrapper.orderByDesc("update_time");
        IPage<QuaCheckPlan> resultPage = this.baseMapper.selectPage(page, queryWrapper);
        if(resultPage.getRecords() != null){
            for (int i = 0; i < resultPage.getRecords().size(); i++) {
                QuaCheckPlan quaCheckPlan = resultPage.getRecords().get(i);
                // 组装制单人名称和审核人名称
                if(quaCheckPlan.getCreateBy() != null){
                    quaCheckPlan.setCreateUser(userService.getById(quaCheckPlan.getCreateBy()).getRealName());
                }
                if(quaCheckPlan.getAuditId() != null){
                    quaCheckPlan.setAuditUser(userService.getById(quaCheckPlan.getAuditId()).getRealName());
                }
            }
        }
        //返回值
        return resultPage;
    }
}




